package com.maoyan.quickdevelop.admin.controller.system;

import cn.dev33.satoken.secure.SaSecureUtil;
import com.maoyan.quickdevelop.common.StringUtils;
import com.maoyan.quickdevelop.common.annotation.Log;
import com.maoyan.quickdevelop.common.constant.HttpStatus;
import com.maoyan.quickdevelop.common.core.AjaxResult;
import com.maoyan.quickdevelop.common.core.domain.DqUser;
import com.maoyan.quickdevelop.common.enums.BusinessType;
import com.maoyan.quickdevelop.common.utils.DateUtils;
import com.maoyan.quickdevelop.system.domain.vo.RegisterVO;
import com.maoyan.quickdevelop.system.service.IDqUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 猫颜
 * @date 2021/5/28 11:21
 * 用户注册
 * TODO 用户注册控制类
 */
@RestController
public class DqRegisterController {
    @Autowired
    private IDqUserService iUserService;

    @ApiOperation(value = "用户注册")
    @PostMapping("/register")
    @Log(title = "用户注册",businessType = BusinessType.INSERT)
    public AjaxResult login(@RequestBody RegisterVO registerVO) {
        DqUser dqUser = new DqUser();
        dqUser.setUserName(registerVO.getUserName());
        //将密码md5加密
        dqUser.setPassWord(SaSecureUtil.md5(SaSecureUtil.sha1(registerVO.getPassWord())));
        dqUser.setEmail(registerVO.getEmail());
        dqUser.setAvatar(registerVO.getAvatar());
        dqUser.setPhoneNumber(registerVO.getPhoneNumber());
        dqUser.setCreateTime(DateUtils.getNowDate());
        dqUser.setUpdateTime(DateUtils.getNowDate());
        dqUser.setLoginDate(DateUtils.getNowDate());

//        if (StringUtils.isNull(registerVO.getPassword())) {
//            return AjaxResult.error(HttpStatus.BAD_REQUEST, "请输入密码");
//        }
//
//        if (StringUtils.isNull(registerVO.getUsername())) {
//            return AjaxResult.error(HttpStatus.BAD_REQUEST, "请输入用户名");
//        }

        //注册
        int i = iUserService.insertDqUser(dqUser);
        //注册成功返回值为1，失败为0
        if (i > 0) {
            return AjaxResult.success("注册成功", i);
        } else {
            return AjaxResult.error("注册失败", i);
        }

    }


}
